package popcorn.ticket.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.dao.MembersMapper;
import popcorn.ticket.dao.UsersMapper;
import popcorn.ticket.entity.Members;
import popcorn.ticket.entity.MembersExample;
import popcorn.ticket.entity.Users;
import popcorn.ticket.entity.UsersExample;
import popcorn.ticket.service.MembersService;
import popcorn.ticket.tool.PageData;

@Service
public class MembersServiceImpl implements MembersService {
	@Autowired
	MembersMapper memMapper;
	@Autowired
	UsersMapper usersmapper;

	// 分页
	@Override
	public PageData<Members> selectMembersByLike(int page, int limit, String keywords) {
		// 判断keywords是否为空
		if (keywords.length() != 0) {
			String name = "%" + keywords + "%";
			// ue方法查询
			UsersExample ue = new UsersExample();
			PageHelper.startPage(page, limit);
			// 获得用户id信息
			ue.createCriteria().andUsernameLike(name);
			List<Users> data = usersmapper.selectByExample(ue);
			int userid = data.get(0).getUserid();
			// 精确查找,先获得会员编号
			int m = memMapper.selectByUserid(userid).getMemberid();
			// 构造方法 赋值内容为 数据 总记录数 limit page
			List<Members> members = memMapper.selectByPrimaryKey(m);
			PageData<Members> pageData = new PageData<>();

			// 获得数据总数
			pageData.setCount(1);
			pageData.setData(members);
			pageData.setLimit(limit);
			pageData.setPage(page);
			// 还有: code 从layui哪里来的
			pageData.setCode(0);
			pageData.setMsg("");
			return pageData;
		}

		// 获得会员信息
		MembersExample me = new MembersExample();
		PageHelper.startPage(page, limit);
		me.createCriteria();
		List<Members> m = memMapper.selectByExample(me);
		// 实例化分页信息
		PageInfo<Members> pageInfo = new PageInfo<Members>(m);
		// 构造方法 赋值内容为 数据 总记录数 limit page
		PageData<Members> pageData = new PageData<>();
		// 获得数据总数
		pageData.setCount((int) pageInfo.getTotal());
		pageData.setData(m);
		pageData.setLimit(limit);
		pageData.setPage(page);
		// 还有: code 从layui哪里来的
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;

	}

	// 修改(根据会员id修改该用户会员信息)
	@Override
	public int updateByPrimaryKeySelective(Members membersid) {
		int n = memMapper.updateByPrimaryKeySelective(membersid);
		return n;
	}

	@Override
	public Members getMember(int userid) {
		int memberid = memMapper.selectByUserid(userid).getMemberid();
		return memMapper.selectByPrimaryKey(memberid).get(0);
	}

	@Override
	public int updateMemberIntegerByUserid(int userid, double integral, String op) {
		// TODO Auto-generated method stub
		return memMapper.updateMemberIntegerByUserid(userid, integral, op);
	}

	// 筛选分页
	@Override
	public PageData<Members> selectMembersByGradeId(int page, int limit, String keywords) {

		// 获得会员信息
		PageHelper.startPage(page, limit);
		int id = Integer.parseInt(keywords);
		List<Members> m = memMapper.selectByGradeId(id);
		// 实例化分页信息
		PageInfo<Members> pageInfo = new PageInfo<Members>(m);
		// 构造方法 赋值内容为 数据 总记录数 limit page
		PageData<Members> pageData = new PageData<>();
		// 获得数据总数
		pageData.setCount((int) pageInfo.getTotal());
		pageData.setData(m);
		pageData.setLimit(limit);
		pageData.setPage(page);
		// 还有: code 从layui哪里来的
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;

	}

}
